<?php
namespace WenwenPay;

use WenwenPay\Config\Config;
use WenwenPay\Exception\PaymentException;
use WenwenPay\Traits\HttpClientTrait;
use WenwenPay\Exception\InvalidConfigException;

class PaymentService
{
    use HttpClientTrait;

    private Config $config;

    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    /**
     * 生成签名
     */
    private function generateSign(array $params): string 
    {
        unset($params['sign']);
        $params = array_filter($params, function($value) {
            return !empty($value) || $value === '0';
        });
        
        ksort($params);
        
        $signStr = '';
        foreach ($params as $key => $value) {
            $signStr .= $key . '=' . $value . '&';
        }
        
        $signStr = substr($signStr, 0, -1) . '&secret_key=' . $this->config->getSecretKey();
        
        return hash('sha256', $signStr);
    }

    /**
     * 发起代付请求
     * 
     * @param array $params {
     *     @type string $id_card_no 身份证号
     *     @type string $bank_card_no 银行卡号
     *     @type string $phone 手机号
     *     @type string $name 姓名
     *     @type string $amount 金额
     *     @type string $client_tx_order_no 商户订单号(可选)
     *     @type string $client_customize_element 自定义参数(可选)
     * }
     * @throws InvalidConfigException
     * @throws PaymentException
     * @return array
     */
    public function transfer(array $params): array
    {
        $requestParams = [
            'agentNo' => $this->config->getAgentNo(),
            'timestamp' => date('YmdHis'),
            'idCardNo' => $params['id_card_no'] ?? '',
            'bankCardNo' => $params['bank_card_no'] ?? '',
            'phone' => $params['phone'] ?? '',
            'name' => $params['name'] ?? '',
            'amount' => $params['amount'] ?? '1',
            'clientTxOrderNo' => $params['client_tx_order_no'] ?? '',
            'clientCustomizeElement' => $params['client_customize_element'] ?? '',
        ];
        
        $requestParams['sign'] = $this->generateSign($requestParams);
        
        return $this->sendRequest($requestParams, 'transfer');
    }

    /**
     * 查询代付订单
     */
    public function query(string $payrollId): array
    {
        $requestParams = [
            'agentNo' => $this->config->getAgentNo(),
            'timestamp' => date('YmdHis'),
            'payrollId' => $payrollId,
        ];
                
        $requestParams['sign'] = $this->generateSign($requestParams);
        
        return $this->sendRequest($requestParams, 'query');
    }

    /**
     * 查询余额
     */
    public function amount(): array
    {
        $requestParams = [
            'agentNo' => $this->config->getAgentNo(),
            'timestamp' => date('YmdHis')
        ];
                
        $requestParams['sign'] = $this->generateSign($requestParams);
        
        return $this->sendRequest($requestParams, 'amount');
    }

    /**
     * 查询列表
     */
    public function list(array $params = []): array
    {
        $requestParams = [
            'agentNo' => $this->config->getAgentNo(),
            'timestamp' => date('YmdHis'),
            'pageNumber' => $params['page_number'] ?? '1',
            'pageLimit' => $params['page_limit'] ?? '10',
            'startDate' => $params['start_date'] ?? date('Y-m-d', strtotime('-7 days')),
            'endDate' => $params['end_date'] ?? date('Y-m-d'),
            'payrollId' => $params['payroll_id'] ?? '',
            'clientTxOrderNo' => $params['client_tx_order_no'] ?? '',
            'clientCustomizeElement' => $params['client_customize_element'] ?? '',
            'dfZt' => $params['df_zt'] ?? '',
            'fhZt' => $params['fh_zt'] ?? '',
        ];

        $requestParams = array_filter($requestParams, function($value) {
            return $value !== '' && $value !== null;
        });
        
        $requestParams['sign'] = $this->generateSign($requestParams);
        
        return $this->sendRequest($requestParams, 'list');
    }
} 